#include "menu_ui.h"

MenuUi::MenuUi(QWidget *parent)
    : QWidget(parent)
{
    this->setMinimumWidth(200);
    this->setMaximumWidth(300);
    this->setAttribute(Qt::WA_StyledBackground, true); // 启用样式表对窗口背景的控制

    // 设置样式表，包括背景颜色
    QString styleSheet = "background-color: #A0A083;"
                         "border-radius: 7px;";
    this->setStyleSheet(styleSheet);
    init();
}
void MenuUi::init()
{
    auto *layout = new QVBoxLayout;

    // 初始化控件
    titleLabel = new QLabel(tr("     menu"));
    titleLabel->setAlignment(Qt::AlignCenter);
    titleLabel->setFixedHeight(40);
    titleLabel->setStyleSheet("color: white;");
    titleLabel->setFont(QFont("Microsoft YaHei", 12, QFont::Bold));
    minimizeButton->setFixedSize(35, 35);
    minimizeButton->setStyleSheet("background-color: #A0A083;"
                                  "border-radius: 7px;");
    setPushButton(minimizeButton, QString::fromUtf8(":/icon/dark_back.png"), "");
    minimizeButton->setFlat(true);

    // 创建菜单栏按钮控件
    setPushButton(newYanChu, QString::fromUtf8(":/icon/zhuye.jpg"), tr(""));
    setPushButton(newZhowBian, tr("Pili周边"));
    setPushButton(newPage1Btn, QString::fromUtf8(":/icon/light_back.png"), tr(""));
    setPushButton(newPage4Btn, QString::fromUtf8(":/icon/logo.png"), tr(""));
    setPushButton(newPage5Btn, QString::fromUtf8(":/icon/light_cart.png"), tr(""));

    // 创建按钮组
    buttonGroup.addButton(newYanChu, 1);
    buttonGroup.addButton(newPage4Btn, 2);
    buttonGroup.addButton(newPage5Btn, 3);
    // 设置按钮的样式
    foreach (QAbstractButton *b, buttonGroup.buttons())
    {
        b->setStyleSheet("background-color: transparent; opacity: 0; border-radius: 7px;");
        b->setFixedHeight(40);
        b->setFont(QFont("Microsoft YaHei", 10));
    }

    // 设置第一个按钮为默认选中状态
    if (QAbstractButton *firstButton = buttonGroup.button(1))
    {
        firstButton->setChecked(true);
    }

    // 添加控件到布局
    auto *titleLayout = new QHBoxLayout;
    titleLayout->addWidget(titleLabel);
    titleLayout->addWidget(minimizeButton);
    layout->addLayout(titleLayout);

    layout->addWidget(newYanChu);
    layout->addWidget(newPage4Btn);
    layout->addWidget(newPage5Btn);

    // 添加一个竖直方向的弹簧
    auto *spacer = new QSpacerItem(10, 10, QSizePolicy::Minimum, QSizePolicy::Expanding);
    layout->addItem(spacer);

    this->setLayout(layout);
    connect(&buttonGroup,
            QOverload<int>::of(&QButtonGroup::idClicked),
            this,
            &MenuUi::buttonClicked);
}
void MenuUi::setPushButton(QPushButton *button, const QString &url, const QString &text)
{
    button->setText(text);
    QIcon icon;
    icon.addFile(url, QSize());
    button->setIcon(icon);
    button->setIconSize(QSize(30, 30));
    button->setCursor(QCursor(Qt::PointingHandCursor));
}
void MenuUi::setPushButton(QPushButton *button, const QString &text)
{
    button->setText(text);
}
MenuUi::~MenuUi() {}
